// pages/product/pages/pinShareOrder/pinShareOrder.js
var http = require('../../../../service/http.js'); //请求类
var util = require('../../../../utils/util.js'); //工具类
const app = getApp();
const Day = 86400000; //毫秒ms
var rd_session = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo: '', //拼团订单信息
    groupPeople: '', //成团人数
    needPeople: '', //差几人成团
    dateFormat: '', //是否以‘天时分秒’显示
    zjGroupList: '', //成团信息

    showProDetail: false, //展示详细拼团信息
    groupDetialAnimation: '', //展示详细信息动画
    shareKey: '', //是否是参与拼团的人打开链接
    isAuthorization: '', //是否已授权登陆
    surplusTime: '', //剩余活动时间（毫秒）
    itemList:'',//订单商品列表
    zjGroupLevelId:'',//拼团id
    product:'',//商品信息
    pinUser:'',//拼单商品信息
    pageName:'pinShareOrder'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('pinShareOrder', options);
    let orderId = ''; //订单id,店铺id，介绍人，是否是拼单进入
    if (options.orderId) {
      this.setData({
        orderId: options.orderId
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    rd_session = wx.getStorageSync('rd_session');
    this.getGroupOrderInfo(this.data.orderId);
    //查看用户是否登陆
    if (app.data.shopInfoTimeOut === true) {//非分享且已经过期，清除店铺信息刷新店铺信息
      app.checkSession();
    }
  },

  getGroupOrderInfo: function(orderId) {
    http.getReq('/weixin/groupActivity/findByOrderId', {
      orderId: orderId || 12019011414325600002,
      rdSession: rd_session,//根据session 获取是否已经参团
    }, (res) => {
      console.log('加载拼团订单成功', res);
      let orderInfo = res.data;
      this.setData({
        orderId: orderId,
        orderInfo: orderInfo,
        needPeople: orderInfo.needPeople,
        zjGroupList: orderInfo.zjGroupActivityPeoples,
        zjGroupLevelId: orderInfo.zjGroupLevelId
      })
      //获取倒计时日期格式
      this.getTimeType(orderInfo.groupEndTime);
      //获取拼团头像列表
      this.getZjGroupList(orderInfo.needPeople);
      //获取商品信息
      this.getItemInfo(orderInfo);
    })

  },
  /**
   * 拼团倒计时格式
   */
  getTimeType: function(groupEndTime) {
    let surplusTime = groupEndTime - (new Date()).getTime();
    console.log('surplusTime', surplusTime);

    this.setData({
      dateFormat: surplusTime > Day ? true : false,
      surplusTime: surplusTime
    })
  },
  /**
   * 获取拼团头像列表
   */
  getZjGroupList: function(needPeople) {
    if (needPeople > 0) {
      let zjGroupList = this.data.zjGroupList;
      for (let i = 0; i < needPeople; i++) {
        zjGroupList.push([]); //补全参人列表
      }
      this.setData({
        zjGroupList: zjGroupList
      })
    }
  },
  //打开详情盒子
  showProDetail: function() {
    //创建动画
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: "ease",
      delay: 0,
      transformOrigin: "50% 50%",
    });
    //第一步先将动画放在要出现之前的位置
    animation.translateY(400).step();
    this.setData({
      groupDetialAnimation: animation.export(),
      isshow: true,
      showProDetail: true
    })

    setTimeout(() => {
      animation.translateY(0).step();
      this.setData({
        groupDetialAnimation: animation.export(),
      })
    }, 50)
  },


  cancelProDetail: function() {
    //创建动画
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: "ease",
      delay: 0,
      transformOrigin: "50% 50%",
    });
    //第一步先将动画放在要出现之前的位置
    animation.translateY(400).step();
    this.setData({
      groupDetialAnimation: animation.export(),
    })

    setTimeout(() => {
      this.setData({
        isshow: false,
        showProDetail: false,
      })
    }, 500)

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    let orderId = this.data.orderId;
    let orderInfo = this.data.orderInfo;
    console.log(res);
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    //shopuid inviter
    //获取当前店铺id 
    let shopInfo = wx.getStorageSync('shopInfo');
    let shopuid = shopInfo.shopuid ? shopInfo.shopuid : '';
    let inviter = wx.getStorageSync('unionid');
    let pinParms = orderId + '/' + inviter + '/' + shopuid + '/0'; //shareKey = 0 拼单用户点击进入
    let product = this.data.product;
    //parms=12019011414325600002/13486/208/1
    return {
      title: '快来' + orderInfo.groupPrice + '起拼' + orderInfo.itemName,
      path: '/pages/home/home?pinParms=' + pinParms,//跳转商品详情页
      imageUrl: product.itemimags[0].thumbImgs
    }
  },
  
  //重组商品信息
  getItemInfo: function (orderInfo) {
    let itemList = orderInfo.itemList;
    this.setData({
      product: orderInfo.itemList[0]
    })
  },
  /**
   * 计时结束
   */
  countStop: function(e) {
    this.setData({
      surplusTime: 0
    })
  },
  //返回到首页
  backToHome: function(e) {
    app.backToHome(this);
  },

})